/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entities.Customers;
import entities.Foods;
import entities.FoodsItem;
import entities.OderFood;
import entities.OrderFoodDetails;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author TANGQUOCTUAN
 */
@Stateful
public class AddToCart implements AddToCartLocal {
    
    ArrayList<FoodsItem> fooditem;

    @PersistenceContext(unitName = "eProject_Tunga_Restaurant-ejbPU")
    private EntityManager em;

    public AddToCart() {
        fooditem = new ArrayList();
    }

    public void addItem(Integer id) {
        Foods food = em.find(Foods.class, id);
        boolean flag = true;
        for (int i = 0; i < fooditem.size(); i++) {
//            FoodsItem fi = ;
            if (fooditem.get(i).getFood().getFoodID().equals(id)) {
                fooditem.get(i).up();
                fooditem.get(i).setPrice(food.getPrice());
                // fooditem.get(i).setSubtotal(fooditem.get(i).getQuantity()*food.getPrice());
                //fooditem.get(i).setTotal(fooditem.get(i).getQuantity()*food.getPrice());
                flag = false;
            }
        }

        if (flag) {
            // lay ra Food
            // Tao FoodsItem
            //add to List
            FoodsItem item = new FoodsItem(food, 1, food.getPrice());
            item.setFood(food);
            fooditem.add(item);
        }
    }

    public void removeItem(Integer id) {
        for (int i = 0; i < fooditem.size(); i++) {
            FoodsItem food = (FoodsItem) fooditem.get(i);
            if (fooditem.get(i).getFood().getFoodID().equals(id)) {
                // food.setQuantity(food.getQuantity()-1);
                // food.setSubtotal(food.getSubtotal()-food.getQuantity()*food.getFood().getPrice());

                if ((fooditem.get(i).getQuantity()) > 1) {
                    fooditem.get(i).setQuantity(fooditem.get(i).getQuantity() - 1);
                } else {
                    fooditem.remove(i);
                }
            }
        }
    }

    public void removeAll() {
        fooditem.clear();

    }

    public void persist(Object object) {
        em.persist(object);
    }

    public ArrayList<FoodsItem> getFooditem() {
        return fooditem;
    }

    public void setFooditem(ArrayList<FoodsItem> fooditem) {
        this.fooditem = fooditem;
    }

    public double totalAmount() {
        double setTotal = 0;
        for (int i = 0; i < fooditem.size(); i++) {
            double a = fooditem.get(i).getPrice();
            double b = fooditem.get(i).getQuantity();
            setTotal += a * b;
        }
        return setTotal;
    }

    public int insertOrderFood(String username,  double totalprice, boolean status) {
        Customers cusobj = em.find(Customers.class, username);
        if (cusobj != null) {
            OderFood oderfoodobj = new OderFood();           
            oderfoodobj.setStatus(status);
            oderfoodobj.setTotalPrice(totalprice);
            oderfoodobj.setDateTime(new Date());
            oderfoodobj.setCustomers(cusobj);
            cusobj.getOderFoodList().add(oderfoodobj);
            em.flush();
            em.refresh(oderfoodobj);           
            return oderfoodobj.getOderFoodID();
        } else {
            return 0;
        }
    }

    public void insertOrderDetails(int oid){
        OderFood oder = em.find(OderFood.class, oid);
        for (int i = 0; i < fooditem.size(); i++) {
                FoodsItem fi = fooditem.get(i);
                Foods f = fi.getFood();
                OrderFoodDetails orderFoodDetails =
                        new OrderFoodDetails(fi.getQuantity(), f.getPrice());
                f = em.find(Foods.class, f.getFoodID());
                orderFoodDetails.setOderFood(oder);
                orderFoodDetails.setFoods(f);
                f.getOrderFoodDetailsList().add(orderFoodDetails);
                oder.getOrderFoodDetailsList().add(orderFoodDetails);
            }
    }
    
}
